package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcCMFoodInspectionMapper;
import com.csun.cmny.provider.dao.OmcCMFoodMapper;
import com.csun.cmny.provider.model.domain.OmcCMFood;
import com.csun.cmny.provider.model.domain.OmcCMFoodInspection;
import com.csun.cmny.provider.model.dto.OmcCMFoodDto;
import com.csun.cmny.provider.model.dto.OmcCMFoodInspectionDto;
import com.csun.cmny.provider.service.OmcCMFoodService;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OmcCMFoodServiceImpl extends BaseService<OmcCMFood> implements OmcCMFoodService {

    @Resource
    private OmcCMFoodMapper omcCMFoodMapper;
    @Resource
    private OmcCMFoodInspectionMapper omcCMFoodInspectionMapper;

    /**
     * 保存食物检查记录
     * @param omcCMFoodInspectionDto
     * @param loginAuthDto
     * @return
     */
    @Override
    public Integer saveInspeciton(OmcCMFoodInspectionDto omcCMFoodInspectionDto, LoginAuthDto loginAuthDto) {
        OmcCMFoodInspection omcCMFoodInspection = new OmcCMFoodInspection();
        BeanUtils.copyProperties(omcCMFoodInspectionDto,omcCMFoodInspection);
        omcCMFoodInspection.setUpdateInfo(loginAuthDto);

        return omcCMFoodInspectionMapper.insertSelective(omcCMFoodInspection);
    }

    /**
     * 查询食物检查记录
     * @param omcCMFoodInspectionDto
     * @return
     */
    @Override
    public List<OmcCMFoodInspection> queryInspection(OmcCMFoodInspectionDto omcCMFoodInspectionDto) {
        if(PublicUtil.isNotEmpty(omcCMFoodInspectionDto.getEndQueryTime())){
            omcCMFoodInspectionDto.setEndQueryTime(omcCMFoodInspectionDto.getEndQueryTime()+" 23:59:59");
        }

        return omcCMFoodInspectionMapper.queryInspection(omcCMFoodInspectionDto);
    }

    /**
     * 保存或者更新食物信息
     * @param omcCMFoodDto
     * @param loginAuthDto
     * @return
     */
    @Override
    public Integer save(OmcCMFoodDto omcCMFoodDto, LoginAuthDto loginAuthDto) {
        OmcCMFood omcCMFood = new OmcCMFood();
        BeanUtils.copyProperties(omcCMFoodDto,omcCMFood);
        omcCMFood.setUpdateInfo(loginAuthDto);
        //新纪录插入，否则更新
        if(PublicUtil.isEmpty(omcCMFood.getId())){
            return omcCMFoodMapper.insertSelective(omcCMFood);
        }else {
            return omcCMFoodMapper.updateByPrimaryKeySelective(omcCMFood);
        }
    }

    /**
     * 查询食物信息
     * @param omcCMFoodDto
     * @return
     */
    @Override
    public List<OmcCMFood> queryFood(OmcCMFoodDto omcCMFoodDto) {
        if(PublicUtil.isNotEmpty(omcCMFoodDto.getEndQueryTime())){
            omcCMFoodDto.setEndQueryTime(omcCMFoodDto.getEndQueryTime()+" 23:59:59");
        }

        return omcCMFoodMapper.queryFood(omcCMFoodDto);
    }
}
